/**
 * @DisplayUtils.as
 * 
 * @author sodaChen mail:asframe@qq.com
 * @version 1.0
 * <br>Copyright (C), 2010-2016 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame
 * <br>Date:2016-5-18
 */
package com.asframe.utils
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	/**
	 * DisplayObject显示对象的辅助方法
	 * @author sodaChen
	 * Date:2016-5-18
	 */
	public class DisplayUtils
	{
		/**
		 * 显示对象绘制成BitmapData对象 
		 * @param target 目标对象
		 * @param rect draw范围
		 * @param isMiniRect 是否最小范围draw（不采用DisplayObject的实际宽高，只用实际有图像的范围）
		 * @return 
		 * 
		 */		
		public static function drawBitmapData(target:DisplayObject,rect:Rectangle = null,isMiniRect:Boolean = true):BitmapData
		{
			//取最小范围
			if(rect == null && isMiniRect)
				rect = target.getBounds(target);
			
			//最大范围
			if(rect == null)
				rect = new Rectangle(0,0,target.width,target.height);
			
			var m:Matrix;
			rect.width = Math.ceil(rect.width);
			rect.width = rect.width == 0 ? 1 : rect.width;
			rect.height = Math.ceil(rect.height);
			rect.height = rect.height == 0 ? 1 : rect.height;
			rect.left = Math.floor(rect.left);
			rect.top = Math.floor(rect.top);
			m = new Matrix();
			m.tx = -rect.left;
			m.ty = -rect.top;
			var bitmapData:BitmapData = new BitmapData(rect.width, rect.height, true, 0);
			bitmapData.draw(target, m);
			
			return bitmapData;
		}
	}
}